import socket  
import struct
import time

def buildframe1(frame_str):
    word = frame_str.split(' ')
    frame_list = []
    for i in word:
        if i.strip()!="":
            a=int(i,16)
            frame_list.append(a)
    return frame_list

def buildframe2(frame_str):
    str_len = len(frame_str)
    frame_list = []
    for i in range(str_len/2):
        a=int(frame_str[i+i:i+i+2],16)
        frame_list.append(a)
    return frame_list

def sendframe1(frame_str):
    frame_list = buildframe1(frame_str)
    for byte in frame_list:
        ss.send(struct.pack('B', byte)) 
        

def sendframe2(frame_str):
    frame_list = buildframe2(frame_str)
    for byte in frame_list:
        ss.send(struct.pack('B', byte))

def showbuf(frame):
    frame_len = len(frame)
    frame_format = '%dB' % frame_len
    aa = struct.unpack(frame_format, frame)
    print frame_len, '->',
    for a in aa:
        print hex(a), ' ',
    print ''
    

print 'start'  
address = ('', 2020)  
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(address)
s.listen(5)  


ss, addr = s.accept()  
print 'got connected from',addr  

while (1):
    sendframe1('EB 90 EB 90 EB 90 71 F4 07 01 02 55 F0 DB CC 3C 6F C9 F1 1B 37 42 C0 B1 F2 B6 6D 1B C6 16 F3 6D 1B 37 6E 2A F4 1C B7 61 72 B3 F5 1B 3F EC 1D BC F6 FC B6 01 00 D4 ')
    sendframe1('EB 90 EB 90 EB 90 71 61 32 01 02 6C 00 \
                31 01 CE 0E 3A 01 99 0F 34 01 80 02 CE 0E 99 0F 08 03 33 01 34 01 0B 04 66 00 03 00 6E 05 00 00 02 00 38 06 32 01 32 01 8E 07 67 00 03 00 DE 08 00 00 00 00 E6 09 9C 02 A5 02 BB 0A A4 00 00 00 85 0B 00 00 00 00 40 0C 9D 02 A5 02 40 0D A6 00 B5 01 89 0E 93 01 DE 00 C6 0F 47 01 24 01 A0 10 B1 00 31 00 AF 11 2B 00 1B 00 2B 12 BE 00 76 00 89 13 A8 00 00 00 24 14 00 00 00 00 42 15 00 00 00 00 20 16 00 00 80 01 37 17 61 01 E2 00 A5 18 00 00 00 00 D4 19 00 00 00 00 B6 1A 00 00 00 00 10 1B 00 00 00 00 72 1C 00 00 00 80 D2 1D 00 80 00 80 BB 1E 00 00 00 00 9F 1F 00 00 3C 03 F1 20 1D 03 54 01 E0 21 34 00 2E 00 70 22 1F 00 AB 01 62 23 C1 01 7C 00 B4 24 42 00 38 00 F2 25 24 00 ED 02 44 26 A6 02 80 01 3C 27 02 00 03 00 A1 28 00 00 00 00 82 29 00 00 00 00 E0 2A 37 01 2D 01 A6 2B 75 00 00 00 58 2C 00 00 00 00 0D 2D 0C 00 08 00 2F 2E 03 00 00 00 F3 2F 00 00 00 00 AB 30 A6 01 A4 01 02 31 89 00 1F 00 C8')
    #sendframe1('EB 90 EB 90 EB 90 71 61 02 07 01 fa 01 b4 01 bc 01 82 02 b4 01 bc 02 2d')
    sendframe1('EB 90 EB 90 EB 90 71 85 04 01 02 CC A0 72 3D 00 00 62 A1 00 00 00 00 6E A2 24 0F 00 00 19 A3 00 00 00 00 AA')
    print 'send'
    time.sleep(5) 

ss.close()  
    
s.close()

print 'end'